-module(state).
-include_lib("nitrogen/include/wf.inc").
-include("src/handling/state.hrl").
-export([make/1]).

build( Module, {OnHeader, OnMenu, OnBody}, State ) ->
    A = case OnHeader of
	    true -> Module:assemble_header(State#state.header);
	    false -> State#state.header
	end,
    B = case OnMenu of
	    true -> Module:assemble_menu(State#state.menu);
	    false -> State#state.menu
	end,
    C = case OnBody of
	    true -> Module:assemble_body(State#state.body);
	    false -> State#state.body
	end,
    #state{header=A, menu=B, body=C}.

wire( Module ) ->
    Module:wire().

make( Xs ) ->
    Final = lists:foldl( fun( {M, A, B, C} , Acc ) ->
				 Out = build(M, {A,B,C}, Acc),
				 wire(M),
				 Out
			 end,
			 #state{},
			 Xs ),
    
    hglobals:vput(helper:id_me(), header, Final#state.header),
    hglobals:vput(helper:id_me(), menu, Final#state.menu),
    hglobals:vput(helper:id_me(), body, Final#state.body),

    [].
